function Rotator(container) {

    this.Items = new Array();
    this.Container = container;
    this.CurrentActive = -1;
    this._fadeSpeed = 400;
    this._changeSpeed = 4000;
    this._lock = false;

    this.PushItem = function () {
        this.Items.push(arguments);
        this.Hide(this.Items.length - 1);
    }

    this.Hide = function (index) {
        for (var i = 0; i < this.Items[index].length; i++) {
            this.Items[index][i].hide();
        }
    }
    this.BuidNavi = function (appendText) {
        this.Container.find('.rotator-navi-switch').remove();
        var $navCont = $('<div class="rotator-navi-switch"></div>');
        for (var i = 0; i < this.Items.length; i++) {
            var $switchItem = $('<a />');
            $switchItem.data('rotator', this);
            $switchItem.data('index', i);
            $switchItem.addClass('rns-i');
            $switchItem.addClass('rns-i' + i);
            if (i == 0)
                $switchItem.addClass('rns-i-first');
            if (i == (this.Items.length - 1))
                $switchItem.addClass('rns-i-last');
            if (appendText)
                $switchItem.text(i);
            $switchItem.appendTo($navCont);

            $switchItem.mouseover(function () {
                $(this).data('rotator').ShowItem($(this).data('index'));
            });

        }
        $navCont.appendTo(this.Container);
    }
    this.ShowItem = function (index) {
        if (index == this.CurrentActive) return;
        this.Container.find('.rns-i').removeClass('rns-i-active');
        this.Container.find('.rns-i' + index).addClass('rns-i-active');
        for (var i = 0; i < this.Items.length; i++) {
            if (i == index)
                this._FadeIn(this.Items[i]);
            else
                this._FadeOut(this.Items[i]);
        }
        this.CurrentActive = index;
    }

    this._FadeIn = function (item) {
        var fs = this._fadeSpeed;
        $(item).each(function () {
            $(this).fadeIn(fs);
        });
    }

    this._FadeOut = function (item) {
        var fs = this._fadeSpeed;
        $(item).each(function () {
            $(this).fadeOut(fs);
        });
    }

    this.NextStep = function () {
        if (!this._lock) {
            var idx = this.CurrentActive + 1;
            if (idx == this.Items.length)
                idx = 0;
            this.ShowItem(idx);
        }
        var rr = this;
        try {
            window.clearInterval(rr.timeout);
        } catch (e) { }
        this.timeout = window.setTimeout(function () { rr.NextStep(); }, this._changeSpeed);
    }

    this.Start = function (startIndex) {
        var rr = this;

        this.Container.mouseover(function () {
            rr._lock = true;
        }).mouseout(function () {
            rr._lock = false;
        });

        if (!startIndex)
            startIndex = Math.floor(Math.random() * 2);

        this.ShowItem(startIndex);

        this.timeout = window.setTimeout(function () { rr.NextStep(); }, this._changeSpeed);
    }

}
